package net.wenku8.reader.ui.lite

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.outlined.KeyboardArrowDown
import androidx.compose.material.icons.outlined.Lock
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import net.wenku8.reader.LocalNavController
import net.wenku8.reader.ui.theme.LiteDark
import net.wenku8.reader.ui.theme.LiteGray


@Composable
fun LiteGroup(text: String) {
    Column(
        modifier = Modifier.liteInline()
    ) {
        LiteText(text)
    }
}

@Composable
fun LiteInput(text : String, data : MutableState<String>, maxLength : Int = 64) {
    LiteLine{
        LiteTextField(
            value = data.value,
            onValueChange = {
                if(it.length <= maxLength){
                    data.value = it
                }
            },
            colors = OutlinedTextFieldDefaults.colors(),
            modifier = Modifier.fillMaxWidth().height(34.dp),
            placeholder = { LiteText(text) },
        )
    }
}

@Composable
fun LitePassword(text : String, data : MutableState<String>, maxLength : Int = 64) {
    LiteLine{
        val showPlaintext = remember { mutableStateOf(false) }
        val visualTransformation = PasswordVisualTransformation()
        LiteTextField(
            value = data.value,
            onValueChange = {
                if(it.length <= maxLength){
                    data.value = it
                }
            },
            colors = OutlinedTextFieldDefaults.colors(),
            modifier = Modifier.fillMaxWidth().height(34.dp),
            visualTransformation = visualTransformation,
            placeholder = { LiteText(text) },
            trailingIcon = {
                IconButton(onClick = {
                    showPlaintext.value = !showPlaintext.value
                }) {
                    val imageVector: ImageVector = if (showPlaintext.value) Icons.Outlined.Lock else Icons.Filled.Lock
                    Icon(imageVector, "icon", modifier = Modifier.iconMinSize())
                }
            }
        )
    }
}

@Composable
fun LiteButton(text: String, showForward : Boolean = false, onClick : () -> Unit) {
    LiteLine(onClick = onClick){
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(30.dp)
                .padding(top = 3.dp),
            horizontalArrangement = Arrangement.Center
        ){
            LiteText(text)
            if (showForward){
                Icon(Icons.Outlined.KeyboardArrowDown, contentDescription = "icon",
                    Modifier.iconMinSize()
                        .padding(top = 2.dp)
                        .graphicsLayer { rotationZ = -90f })
            }
        }
    }
}


@Composable
fun LiteTextButton(text: String, onClick : () -> Unit) {
    Row(
        modifier = Modifier
            .smallPadding()
            .padding(start = 5.dp, end = 5.dp)
            .background(LiteGray)
            .border(width = 1.dp, color = LiteDark, shape = CutCornerShape(3.dp))
            .onClick { onClick() },
        horizontalArrangement = Arrangement.Center
    ){
        Row(
            modifier = Modifier.padding(start = 5.dp, end = 5.dp)
        ) {
            LiteText(text)
        }
    }
}


@Composable
fun LiteLine(onClick : () -> Unit = {}, content: @Composable () -> Unit) {
    Column(
        modifier = Modifier.liteOutline()
            .onClick { onClick() }
    ) {
        Row(
            modifier = Modifier.fillMaxWidth().liteInline(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            content()
        }
    }
}

@Composable
fun LiteTextLink(text: String, indent : Int = 0, header: @Composable () -> Unit = {}, onClick : () -> Unit) {
    LiteLine(onClick = onClick){
        header()
        LiteText(text, indent = indent)
    }
}

@Composable
fun LiteBlank() {
    Row(
        modifier = Modifier.height(10.dp)
    ) {

    }
}

@Composable
fun LiteTextFold(title: String, detail: String){
    var show by remember { mutableStateOf(false) }
    LiteLine(
        onClick = {
            show = !show
        }
    ) {
        Column {
            Row {
                Icon(Icons.Outlined.KeyboardArrowDown, contentDescription = "icon",
                    Modifier.iconSize().graphicsLayer {
                        rotationZ = if (show) 0f else -90f
                    })
                LiteLabel(text = title)
            }
            LiteText(text = detail, maxLines = if (show) Int.MAX_VALUE else 2)
        }
    }
}

@Composable
fun LiteCardFold(header: @Composable () -> Unit, content: @Composable () -> Unit){
    var show by remember { mutableStateOf(false) }
    Column {
        LiteLine(
            onClick = {
                show = !show
            }
        ){
            Row{
                Icon(Icons.Outlined.KeyboardArrowDown, contentDescription = "icon",
                    Modifier.iconSize().graphicsLayer {
                        rotationZ = if (show) 0f else -90f
                    })
                header()
            }
        }
        if (show){
            content()
        }
    }
}


@Composable
fun LiteCard(onClick : () -> Unit = {},
             left: @Composable () -> Unit = {},
             indent: Int = 10,
             header: @Composable () -> Unit = {},
             content: @Composable () -> Unit) {
    LiteLine(onClick = onClick){
        Row{
            left()
            Column(
                modifier = Modifier.padding(start = (indent).dp, end = (indent).dp)
            ) {
                Column(
                    modifier = Modifier.padding(top = 3.dp)
                ) {
                    header()
                }
                Column(
                    modifier = Modifier.padding(top = 3.dp)
                ) {
                    content()
                }
            }
        }

    }
}

@Composable
fun LitePanel(hostState: SnackbarHostState? = null,
              title: String = "",
              backNav: Boolean = false,
              actions: @Composable RowScope.() -> Unit = {},
              topBar: Boolean = true,
              dataLoading : MutableState<Boolean> = mutableStateOf(false),
              content: @Composable () -> Unit
){
    DisposableEffect(Unit) {
        onDispose {
            hostState?.currentSnackbarData?.dismiss()
        }
    }
    Scaffold(
        snackbarHost = { if (hostState != null) LiteSnackbarHost(hostState = hostState) },
        topBar = {
            LitePanelTopBar(title, backNav, actions, topBar, dataLoading)
        },
    ) {
        Surface(
            modifier = Modifier.padding(top = it.calculateTopPadding())
        ){
            if (!dataLoading.value){
                content()
            }
        }
    }
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LitePanelTopBar(title : String,
                    backNav : Boolean = false,
                    actions: @Composable RowScope.() -> Unit = {},
                    topBar: Boolean = true,
                    initLoading : MutableState<Boolean>? = null,
                    ){
    val navController = LocalNavController.current
    Column(
        modifier = Modifier.sizeIn(minHeight = 1.dp)
    ){
        if (topBar){
            CenterAlignedTopAppBar(
                colors = TopAppBarDefaults.topAppBarColors(
                    containerColor = LiteGray,
                    titleContentColor = Color.Black,
                ),
                title = {
                    LiteTitle(text = title)
                },
                navigationIcon = {
                    if (backNav){
                        IconButton(onClick = {
                            navController.popBackStack()
                        }) {
                            Icon(
                                modifier = Modifier.iconSize(),
                                imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                                contentDescription = null
                            )
                        }
                    }
                },
                actions = {
                    Row(
                        modifier = Modifier.padding(end = 16.dp)
                    ){
                        actions()
                    }
                },
                scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(),
                modifier = Modifier.padding(bottom = 1.dp)
            )
        }
        if (initLoading != null && initLoading.value){
            LinearProgressIndicator(
                modifier = Modifier.fillMaxWidth().height(1.dp),
                color = Color.LightGray,
                gapSize = 0.dp
            )
        }
    }
}


